package apply;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import theBeans.*;
import theDaoFactory.DaoFactory;
import theDaos.IExamDao;
import theDaos.IExamInfoDao;
import theDaos.IPositionDao;
import theDaos.IStudentDao;

/**
 * Servlet implementation class checkServlet
 */
@WebServlet("/checkServlet")
public class checkServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public checkServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session=request.getSession();
		exam newExam=new exam();
		newExam=(exam) session.getAttribute("newExam");
		
		IExamInfoDao examInfoDaoInstance=DaoFactory.getExamInfoDaoInstance();
		examInfo examInfoOption=new examInfo();
		examInfoOption=examInfoDaoInstance.findByEid(String.valueOf(newExam.getEid()));
		
		IPositionDao positionDaoInstance=DaoFactory.getPositionDaoInstance();
		position positionOption=new position();
		positionOption=positionDaoInstance.findByPid(String.valueOf(newExam.getPid()));
		
		IStudentDao studentDaoInstance=DaoFactory.getStudentDaoInstance();
		student studentOption=new student();
		studentOption=studentDaoInstance.findBySid(String.valueOf(newExam.getSid()));
		
		if(positionOption.getIfEmpty()==1&&studentOption.getCredit()!=0)
		{
			//将分配了的pid的ifEmpty属性置为0
			int pid=newExam.getPid();
			int result=0;
			int ifEmpty=0;
			result=positionDaoInstance.update(String.valueOf(pid),String.valueOf(ifEmpty));
			ServletContext application=request.getServletContext();
			int positionLeft=((Integer)application.getAttribute("positionLeft"))-1;
			application.setAttribute("positionLeft", positionLeft);
			
			SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//根据当前时间分配准考证号
			String identityNum = df.format(new Date());
			System.out.println(identityNum);
			newExam.setIdentityNum(Integer.parseInt(identityNum.substring(0, 8)));
			
			IExamDao examDaoInstance=DaoFactory.getExamDaoInstance();
			examDaoInstance.update(String.valueOf(newExam.getEid()), String.valueOf(newExam.getSid()), String.valueOf(newExam.getPid()), String.valueOf(newExam.getIdentityNum()));
			
			session.setAttribute("newExam", newExam);
			response.sendRedirect("pay.jsp");
		}
		else
		{
			request.setAttribute("errorInfo", "");
			request.setAttribute("toURL", "");
			request.getRequestDispatcher("errorPage.jsp").forward(request, response);
		}
		
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
